package stattracker;

import java.awt.FileDialog;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class SaveComponentListener extends MouseAdapter{
	
	private JComponent save;
	private JFrame parent;
	
	public SaveComponentListener(JComponent toSave, JFrame parent)
	{
		save = toSave;
		this.parent = parent;
	}
	
	public void mouseClicked(MouseEvent me)
	{
		if(SwingUtilities.isRightMouseButton(me))
		{
			int n = JOptionPane.showConfirmDialog(parent,
					"Do you want to save this section as an image?",
					"Save Image?",
					JOptionPane.YES_NO_OPTION);
			if(n == JOptionPane.YES_OPTION)
			{
				FileDialog fd = new FileDialog(parent, "Save", FileDialog.SAVE);
		        fd.setVisible(true);
		        if (MyUtils.emptyString(fd.getFile()))
		            return;
		        String fileLoc = fd.getDirectory() + fd.getFile();
		        if(!fileLoc.endsWith(".jpg"))
		        {
		        	fileLoc += ".jpg";
		        }
		        MyUtils.saveComponentAsJPEG(save, new File(fileLoc));
			}
			parent.repaint();
		}
	}
}
